<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class compte_ctr extends CI_Controller {  

	    function __construct()
    {
        parent::__construct();
        
		$this->load->helper(array('form', 'url'));
	
        $this->load->helper('form'); 
 
    }
	  
	 function index()
	{
		$user_name = $this->session->userdata('user_name');	
		$group = $this->session->userdata('user_group');
		//if($group=='admin'){
			$data['msg']=NULL;
			$this->load->view('compte_admin_vew',$data);  // send the result from the model to the view 
		//}else{
		//	$this->load->view('compte_admin_vew',$data);  // send the result from the model to the view 
		//}
	}
	
	function changer_password(){
		$ancien_pw= $this->input->post('ancien_pw');
		$nouveau_pw= $this->input->post('new_pw');
		$confirmer_pw= $this->input->post('confirmation');
		$nom = $this->session->userdata('user_name');
		
		$this->load->model('users_mdl');	
	    $data= $this->users_mdl->get_user($nom);
		if($ancien_pw==$data[0]->pass){
			// ancien mot de pass correct
			if($nouveau_pw==$confirmer_pw){
				$id=$data[0]->id_user;
				$this->users_mdl->change_pw($id,$nouveau_pw);
				$this->load->view('main_vew');
			}else{
				// les mot de pass ne correspond pas
				$data['msg']='تاكيد كلمة السر غير مطابق';
				$this->load->view('compte_admin_vew',$data);  // send the result from the model to the view 
			}
		}else{
			// mot de pass incorrecte
			$data['msg']='كلمة السر خاطئة';
			$this->load->view('compte_admin_vew',$data);  // send the result from the model to the view 
		}
		$v=0;
	}
	
		function new_compte($msg){
		
		$this->load->model('users_mdl');	
	     $data['users']= $this->users_mdl->get_users();
		 $data['groups']= $this->users_mdl->get_groups();
		 $data['msg']=$msg;
		 
		$this->load->view('compte_new_vew',$data);  
	}

     function ajout_new_compte(){
		
		$this->load->model('users_mdl');
			
	    $nom= $this->input->post('nom');
		$prenom= $this->input->post('prenom');
		$pseudo= $this->input->post('pseudo');
		$pw= $this->input->post('pw');
		$confirmation= $this->input->post('confirmation');
		$group= $this->input->post('group');
		$msg="";
		$existe_pseudo=$this->users_mdl->verifie_pseudo($pseudo);
		if($pw!=$confirmation)$pw_incorrect=TRUE;
		else $pw_incorrect=FALSE;
		
		if($existe_pseudo==0 and $pw_incorrect==FALSE){
			$this->users_mdl->ajout_user($nom,$prenom,$pseudo,$pw,$group);
			$this->load->view('budget_vew');

		}else{
			if($pw_incorrect)$msg="  كلمة الدخول غير مطابقة , ";
			if($existe_pseudo)$msg=$msg." كنية مستعلملة ";
			$this->new_compte($msg);
		}
		
		//$this->load->view('compte_new_vew',$data);  
	}
		
		
	}
	
	
